package com.youhui.weixin.base;

import com.youhui.weixin.dao.GoodsDao;
import com.youhui.weixin.dao.GoodsLinkDao;
import com.youhui.weixin.domain.Goods;
import com.youhui.weixin.domain.GoodsLink;
import com.youhui.weixin.utils.excel.DealType;
import groovy.util.logging.Log4j;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by https://github.com/kuangcp on 17-11-14  下午7:32
 * 并不能用
 * @author kuangcp
 */

public class TransLink implements Runnable {

    private GoodsDao goodsDao;
    private GoodsLinkDao goodsLinkDao;
    private DealType dealType;
    private int page;
    private int size = 200;
    private Logger log = LoggerFactory.getLogger(TransLink.class);

    public TransLink(GoodsDao goodsDao, GoodsLinkDao goodsLinkDao, DealType dealType, int page) {
        this.goodsDao = goodsDao;
        this.goodsLinkDao = goodsLinkDao;
        this.dealType = dealType;
        this.page = page;
//        this.size = size;
    }

    @Override
    public void run() {
        Pageable pageable = new PageRequest(page, size);
        Page<Goods> goods = goodsDao.findListByLink(pageable);

        Set<String> set = new HashSet<>();
        for (Goods g : goods) {
            String s = dealType.handleLink(g.getGoodsId(), g.getLink());
            goodsLinkDao.save(new GoodsLink(g.getGoodsId(), g.getLink()));
            g.setLink("http://wx.jjyouhuigo.com/s/"+s);
            goodsDao.save(g);
            log.info("http://wx.jjyouhuigo.com/s/"+s);
            set.add(s);
        }
        System.out.println("Goods:" + goods.getSize());
        System.out.println("Set:" + set.size());

    }


}
